import Vue from "vue";
import axios from "axios";

//原型肾功能挂载请求方式
Vue.prototype.$axios = axios;

//跳转文件页面
Vue.prototype.$$downLoadFile = function (param, fun) {
    //需要测试这也方法的先后顺序
    let that = this;
    return new Promise((resolve, reject) => {
        that.http.get(param.path = that.$api.processingApi.isExit, param.params).then((res) => {
            if (res.data.code === 0) {
                if (fun) {
                    fun();
                }
                resolve(res);
            } else {
                resolve(res);
                that.$Message.warning("无下载数据");
            }
        }).catch((res) => {
            reject(res);
        });
    });
};


// Vue.prototype.$$openDownLoadFile = function (downLoadFile) {
//     //downLoadFile="DownloadFile://TEeLAfnvTC0y0WtIUb5M9WOtYq8Elykb/pieadmin/pie/data/247/sthx_user_data/pieadmin/lilintest/123/GF3_KAS_QPSI_010970_E81.2_N40.5_20180910_L1A_VH_L10003442728.tif/"
//     if (downLoadFile.slice(0,15)==="DownloadFile://") {
//         window.protocolCheck(  downLoadFile, function () {
//             let  r=window.confirm("未发现文件下载器，是否下载安装？");
//             if (r) {
//                 let aData=document.createElement("a");
//                 aData.setAttribute("href", "./pdf/setup_20210309.exe");
//                 aData.setAttribute("download", "setup_20210309.exe");
//                 aData.setAttribute("id",  "downloadll");
//                 window.document.body.appendChild(aData);
//                 document.getElementById("downloadll").click();
//                 window.document.body.removeChild(document.getElementById("downloadll"));
//             }
//         });
//     }else {
//         this.$Message.warning("下载路径错误，请联系管理员！");
//     }
// };



Vue.prototype.$$openDownLoadFile = function (downLoadFile) {
    // downLoadFile = "DownloadFile://TEeLAfnvTC0y0WtIUb5M9WOtYq8Elykb/pieadmin/pie/data/247/sthx_user_data/pieadmin/lilintest/123/GF3_KAS_QPSI_010970_E81.2_N40.5_20180910_L1A_VH_L10003442728.tif/"
    if (downLoadFile.slice(0, 15) === "DownloadFile://") {
        window.protocolCheck(downLoadFile, function () {
            let r = window.confirm("未发现文件下载器，是否下载安装？");
            if (r) {
                // let root = this.$api.manageApi.downloadFilePath;
                let root = "./pdf/";
                let path = this.$api.manageApi.getByDictCode + "downloadFilePath";
                this.http.get(path).then((res) => {
                    if (res.data.success) {
                        if (res.data.data.length < 1) return;
                        let downloadFileHref = root + res.data.data[0].dictName;
                        let downloadFileName = "文件管理工具_" + res.data.data[0].dictName;
                        dealwithDownload(downloadFileHref, downloadFileName);
                    } else {
                        this.$Message.error(res.data.message);
                    }
                });
            }
        });
    } else {
        this.$Message.warning("下载路径错误，请联系管理员！");
    }
};

let dealwithDownload = function (path, fileName) {
    let aData = document.createElement("a");
    // aData.setAttribute("href", "./pdf/setup_20210309.exe");
    // aData.setAttribute("download", "setup_20210309.exe");
    aData.setAttribute("href", path);
    aData.setAttribute("download", fileName);
    aData.setAttribute("id", "downloadll");
    window.document.body.appendChild(aData);
    document.getElementById("downloadll").click();
    window.document.body.removeChild(document.getElementById("downloadll"));

}


Vue.prototype.$$findCorn = function $$findCorn(dataList, courentItem, name = "dictName") {
    if (typeof dataList === "object") {
        let courentData = dataList.find((item) => {
            return item.dictCode === courentItem;
        });
        if (courentData) {
            return courentData[name];
        }
    }
};


//自定义指令用于获取元素的宽高 和屏幕高宽
/*
 * 使用方法  v-resize.quiet="getTableHeight"
 * */
Vue.directive("resize", {
    inserted(el, binding) {
        const callback = binding.value; //指令的值
        const modifiers = binding.modifiers; //指令的修饰符
        let boxWidth = getComputedStyle(el, null)["width"];
        let boxHeight = getComputedStyle(el, null)["height"];
        const onResize = () => callback(boxWidth, boxHeight, window.innerWidth, window.innerHeight);
        window.addEventListener("resize", onResize);
        if (modifiers.quiet) {
            onResize();
        }
        el._onResize = onResize;
    },
    unbind(el) {
        if (!el._onResize) return;
        window.removeEventListener("resize", el._onResize);
        delete el._onResize;
    }
});




/*
  毫秒转为正常格式时间过滤器
  编写过滤器传入需要的时间格式，例如：yyyy-MM-dd hh:mm:ss或者yyyy年MM月dd日
  注意：月MM必须大写，目的为了区分月与分，其他都为小写
  时间格式定义：
    参数为0格式：yyyy-MM-dd hh:mm:ss
    参数为1格式：yyyy-MM-dd
    参数为2格式：hh:mm:ss
    参数为3格式：yyyy年MM月dd日 hh时mm分ss秒
    参数为4格式：yyyy年MM月dd日
    参数为5格式：hh时mm分ss秒
    参数为6格式：yyyy年MM月
    参数为7格式：yyyy
    参数为8格式：yyyy/MM/dd
*/
/* 一位数两位数转换 */
let padLeftZero = (str) => {
    return ('00' + str).substr(str.length);
};
Vue.prototype.formatDate = (date, fmt) => {
    var date = new Date(date);

    if (fmt == 0) {
        fmt = "yyyy-MM-dd hh:mm:ss"
    } else if (fmt == 1) {
        fmt = "yyyy-MM-dd"
    } else if (fmt == 2) {
        fmt = "hh:mm:ss"
    } else if (fmt == 3) {
        fmt = "yyyy年MM月dd日 hh时mm分ss秒"
    } else if (fmt == 4) {
        fmt = "yyyy年MM月dd日"
    } else if (fmt == 5) {
        fmt = "hh时mm分ss秒"
    } else if (fmt == 6) {
        fmt = "yyyy年MM月"
    } else if (fmt == 7) {
        fmt = "yyyy"
    } else if (fmt == 8) {
        fmt = "yyyy/MM/dd"
    } else if (fmt == 9) {
        fmt = "hh:mm"
    }else if (fmt == 10) {
        fmt = "MM-dd hh:mm"
    }
    if (/(y+)/.test(fmt)) {
        fmt = fmt.replace(RegExp.$1, (date.getFullYear() + '').substr(4 - RegExp.$1.length));
    }
    let o = {
        'M+': date.getMonth() + 1,
        'd+': date.getDate(),
        'h+': date.getHours(),
        'm+': date.getMinutes(),
        's+': date.getSeconds()
    };
    for (let k in o) {
        if (new RegExp(`(${k})`).test(fmt)) {
            let str = o[k] + '';
            fmt = fmt.replace(RegExp.$1, (RegExp.$1.length === 1) ? str : padLeftZero(str));
        }
    }
    return fmt;
};
